<?php
/**
 * 道具服务模块
 * 
 * @author river_he@eueang.com
 */
include_once(AMFPHP_BASE . "shared/util/MethodTable.php");
final class ProperService extends Kit_Service{
	/**
	 * 获取道具列表（按热度排行）
	 * 
	 * @param int $page 页码
	 * @param int $psize 页记录数
	 * @return array
	 */
	public function getProps($page=1, $psize=10){
		$page = $page>0?$page:1;
		$psize = $psize>0?$psize:10;
		$key = __FUNCTION__.'_'.$page.'_'.$psize;
		$key = $this->getCacheKey($key);
		$props = false; //$this->getMemcache()->get($key);
		if($props === false){
			$props = $this->getPropMode()->getItems('nOnShelf=1', 'nHeat ASC', $page, $psize);
			if(!$props){
				return $this->pack(201, 'page not found');
			}
			//$this->getMemcache()->set($key, $props, 10);
		}
		return $this->pack(200, 'OK', $props);
	}
	
	/**
	 * 卖出道具
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shoperPropId 店主道具id
	 * @return array
	 */
	public function sell($shoperId, $shoperPropId){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1 > $shoperPropId){
			return $this->pack(102, 'argument shoperPropId error');
		}
		$sell = $this->getPropMode()->sell($shoperId, $shoperPropId);
		if(0 < $sell){
			return $this->pack(200, 'OK');
		}else{
			$code = 0;
			$message = '';
			switch($sell){
				case -1:
					$code += 201;
					$message .= 'no property'; 
					break;
				case -2:
					$code += 202;
					$message .= 'shoper id wrong';
					break;
				case -3:
					$code += 203;
					$message .= '该道具已经使用或已经卖出';
					break;
				case -4:
					$code += 204;
					$message .= 'shoper id wrong';
					break;
				case -5:
					$code += 205;
					$message .= 'operation error';
					break;
				case -6:
					$code += 206;
					$message .= 'server error';
					break;
			}
			return $this->pack($code, $message);
		}
		return $this->pack(501, 'server error');
	}
	
	/**
	 * 购买道具
	 * 
	 * @param int $shoperId 店主id
	 * @param int $propId 道具id
	 * @param int $total 购买数量
	 * @return array
	 */
	public function buy($shoperId, $propId, $total=1){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1 > $propId){
			return $this->pack(102, 'argument shopId error');
		}elseif(1 > $total){
			return $this->pack(102, 'argument total error');
		}
		$buy = $this->getPropMode()->buy($shoperId, $propId, $total);
		if(is_array($buy)){
			return $this->pack(200, 'OK', $buy);
		}else{
			$message = '';
			$code = 0;
			switch($buy){
				case -1:
					$message .= 'property not found';
					$code = 201;
					break;
				case -2:
					$message .= 'shoper not found';
					$code = 202;
					break;
				case -3:
					$message .= '道具定价错误';
					$code = 205;
					break;
				case -4:
					$message .= '金币不足';
					$code = 206;
					break;
				case -5:
					$message .= '美食币不足';
					$code = 207;
					break;
				case -6:
					$message .= '数据处理错误';
					$code = 208;
					break;
				case -7:
					$message .= '数据处理错误';
					$code = 209;
					break;
			}
			return $this->pack($code, $message);
		}
	}
}
//end